<template>
  <el-dialog
    :title="optionType==='see'?'查看':(optionType==='edit'?'编辑':(optionType==='add'?'添加':'提示'))"
    :visible.sync="dialogVisible"
    :before-close="close"
    :close-on-click-modal="false"
    append-to-body
    width="80%"
  >
    <div>
      <el-form ref="form" :model="formData" label-width="130px">
        <el-form-item label="标签名">
          <el-input
            v-model="formData.dictionaryLabel"
            :disabled="optionType==='see' || (optionType==='edit' && false)"
          />
        </el-form-item>
        <el-form-item label="标签值">
          <el-input
            v-model="formData.dictionaryValue"
            :disabled="optionType==='see' || (optionType==='edit' && false)"
          />
        </el-form-item>
        <el-form-item label="标签描述">
          <el-input
            v-model="formData.dictionaryDescribe"
            :disabled="optionType==='see' || (optionType==='edit' && false)"
          />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <div v-if="optionType==='edit' || optionType==='add'">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
      <el-button v-else type="primary" @click="close">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getSysDictDetailById, updateSysDictDetailById, saveSysDictDetailData } from '@/api/modules/system/sysDictApi'
export default {
  name: 'Edit',
  data() {
    return {
      optionType: '',
      dialogVisible: false,
      formData: {
        sysDictId: null
      },
      // 主键
      optionId: null,
      mainTableId: null
    }
  },
  methods: {
    open(option) {
      this.optionType = option.optionType
      this.optionId = option.optionId
      this.mainTableId = option.mainTableId
      this.formData.sysDictId = this.mainTableId
      this.dialogVisible = true
      if (this.optionType !== 'add') {
        this.getByIdData()
      }
    },
    close() {
      this.formData = {
        sysDictId: this.mainTableId
      }
      this.dialogVisible = false
    },
    save() {
      if (this.optionType === 'add') {
        saveSysDictDetailData(this.formData).then(res => {
          this.$message.success('添加成功')
          this.close()
          this.$emit('refreshPageData')
        })
      } else if (this.optionType === 'edit') {
        updateSysDictDetailById(this.formData).then(res => {
          this.$message.success('修改成功')
          this.close()
          this.$emit('refreshPageData')
        })
      }
    },
    getByIdData() {
      getSysDictDetailById(this.optionId).then(res => {
        this.formData = res.obj
      })
    }
  }
}
</script>

<style scoped>

</style>
